package aapilast2;

public class SyncDemo4 {
    public static void main(String[] args) {
        Foo f = new Foo();
        new Thread(f::method1).start();
        new Thread(f::method2).start();
    }
}

class Foo {
    public synchronized void method1() {
        Thread t = Thread.currentThread();
        try {
            System.out.println(t.getName() + "正在执行method1方法...");
            Thread.sleep(3000);
            System.out.println(t.getName() + "method1方法执行完毕...");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public synchronized void method2() {
        Thread t = Thread.currentThread();
        try {
            System.out.println(t.getName() + "正在执行method2方法...");
            Thread.sleep(3000);
            System.out.println(t.getName() + "method2方法执行完毕...");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}